﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using KCL.Code.Entities;

namespace KCL.Code.Data
{
    public class CompanyDAO : BaseDAO
    {
        public Company GetCompanyInfo()
        {
            using (SqlConnection connection = GetConnection())
            {
                string sqlStatement = string.Format("SELECT * FROM [Company]");
                SqlCommand myCommand = new SqlCommand(sqlStatement, connection);
                SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
                DataTable dt = new DataTable();

                try
                {
                    connection.Open();
                    myAdapter.Fill(dt);
					Company company = new Company();

                    int currentIndex = 0;
                    if (dt.Rows.Count == 1)
                    {
                        company.id = Convert.ToInt32(dt.Rows[currentIndex]["id"]);
                        company.name = dt.Rows[currentIndex]["name"].ToString();
                        company.address = dt.Rows[currentIndex]["address"].ToString();
                        company.phone = dt.Rows[currentIndex]["phone"].ToString();
                        company.mobile = dt.Rows[currentIndex]["mobile"].ToString();
                        company.fax = dt.Rows[currentIndex]["fax"].ToString();
                        company.accountNumber = dt.Rows[currentIndex]["accountNumber"].ToString();
                        company.bank = dt.Rows[currentIndex]["bank"].ToString();
                        company.taxNumber = dt.Rows[currentIndex]["taxNumber"].ToString();
                        company.director = dt.Rows[currentIndex]["director"].ToString();
                        company.logo = dt.Rows[currentIndex]["logo"].ToString();
						return company;
                    }
                    return null;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public bool EditCompany(Company company)
        {
            using (SqlConnection connection = GetConnection())
            {
                string sqlStatement = string.Format("UPDATE [Company] SET [name] = N'{0}', [address] = N'{1}', [phone] = N'{2}', [mobile] = N'{3}', [fax] = N'{4}', [accountNumber] = N'{5}', [bank] = N'{6}', [taxNumber] = N'{7}', [director] = N'{8}', [logo] = N'{9}'", company.name, company.address, company.phone, company.mobile, company.fax, company.accountNumber, company.bank, company.taxNumber, company.director, company.logo);
				sqlStatement = sqlStatement.Replace("N''", "NULL").Replace(", [logo] = NULL", "");
				SqlCommand myCommand = new SqlCommand(sqlStatement, connection);

                try
                {
                    connection.Open();
                    return (myCommand.ExecuteNonQuery() != 0);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }
}